package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.awt.*;
import java.util.List;
import java.util.Set;


@Slf4j
@RestController
@RequestMapping("/admin/dish")
public class DishController {
    @Autowired
    DishService dishService;
    @Autowired
    RedisTemplate redisTemplate;
    @GetMapping("/page")
    public Result<PageResult> queryDishPage(DishPageQueryDTO dishPageQueryDTO){
       PageResult page =   dishService.queryDishPage(dishPageQueryDTO);
       return Result.success(page);
    }
    @GetMapping("/{id}")
    public Result<DishVO> queryCategoryById(@PathVariable long id){
        DishVO category=  dishService.queryCategoryById(id);
        return  Result.success(category);
    }

    @PostMapping("/status/{status}")
    public Result setStatus(@PathVariable Integer status,@RequestParam Long id){
        dishService.changeStatusById(id,status);
        return Result.success();
    }

    @PostMapping
    @Transactional // 开启事务管理  要么全成功要么全失败
    public Result updateDish(@RequestBody DishDTO dishDTO){
        System.out.println(dishDTO);
        dishService.insetDish(dishDTO);
        return Result.success("修改成功～");
    }

    @PutMapping
    public Result updateDish(@RequestBody DishVO vo){
            dishService.updateDish(vo);
        return Result.success("");

    }
    @DeleteMapping
    public Result delDish( @RequestParam List<Long> ids){
        dishService.deleteDish(ids);
        // redis中删除对应的key
        Set keys = redisTemplate.keys("DISH_*");
        redisTemplate.delete(keys);
        return Result.success();
    }
}
